package com.chain.qs.modules.biz.converter;

import com.chain.qs.common.persistence.DataEntity;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

/**
 * Created by cheshire_cat on 16/3/25.
 */
public class StringToEntityFactory implements ConverterFactory<String,DataEntity> {

    @Override
    public <T extends DataEntity> Converter<String, T> getConverter(Class<T> aClass) {
        return new StringToEntity<T>(aClass);
    }

    static class StringToEntity<T extends DataEntity> implements Converter<String,T>{

        Class<T> clazz;

        public StringToEntity(Class<T> clazz) {
            this.clazz = clazz;
        }

        @Override
        public T convert(String s) {
            T t =null;
            try {
                t =clazz.newInstance();
                t.setId(s);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return  t;
        }
    }
}
